﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginByAjax.aspx.cs" Inherits="LoginByAjax" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>用户登录</title>
   <link rel="stylesheet" type="text/css" href="css/common.css" />
    <script src="Scripts/common.js" type="text/javascript"></script>
   <script type="text/javascript">
       function gel(id) { return document.getElementById(id); }
       var xhr;
       window.onload = function () {
           //alert(gel("btnL").value);
           gel("fLogin").onsubmit = validate;
         
           xhr = createXmlHttp();
           gel("btnL").onclick = LoginByAjax;
       }
       function validate() {
           var uName = gel("txtUName");
           var uPwd = gel("txtPwd");
           var reg = /^[0-9a-zA-Z]{6,16}$/;
           if (uName.value == "" || !reg.test(uName.value)) {
               uName.focus();
               gel("msgUname").className = "errMsg";
               return false;
           } else { gel("msgUname").className = "hid"; }
           if (uPwd.value == "" || !reg.test(uPwd.value)) {
               uPwd.focus();
               gel("msgPwd").className = "errMsg";
               return false;
           } else {
               gel("msgPwd").className = "hid";
           }
           gel("fLogin").submit();
       }

       function ReVCode() {
           gel("vcode").src = "VCode.ashx?time=" + new Date().getDate;
       }

       function LoginByAjax() {
           var uName = gel("txtUName").value;
           var uPwd = gel("txtPwd").value;
           var data = "uName=" + uName + "&uPwd=" + uPwd;
           var strUrl = "LoginByAjax.aspx";
           xhr.open("POST", strUrl, true);
           xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
           xhr.onreadystatechange = function () {
               if (xhr.readyState == 4) {
                   if (xhr.status == 200) {
                       var txt = xhr.responseText;
                       if (txt == "1") {
                           alert("登录成功");
                           window.location = "Default.aspx";
                       }
                       else {
                           alert("登录失败");
                       }
                   }
                   else {
                       alert("服务器出错");
               }
               
               }
           }
           xhr.send(data);
              }


//       function loginByAjax() {
//           var uName = gel("txtUName").value;
//           var uPwd = gel("txtPwd").value;
//           var urlStr = "LoginByAjax.aspx";
//           var data = "un=" + uName + "&up=" + uPwd;
//           xhr.open("POST", urlStr, true);
//           xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//           xhr.onreadystatechange = function () {
//               //gel("msgDiv").innerHTML = "登录中......";
//               if (xhr.readyState == 4) {
//                   if (xhr.status == 200) {
//                       var reponseText = xhr.responseText;
//                       if (reponseText == "1") {
//                           alert("登录成功！");
//                           window.location = "Default.aspx";
//                       } else {
//                           alert("登录失败！");
//                       }
//                   }
//                   else {
//                       alert("服务器错误！xhr.status=" + xhr.status);
//                   }
//               }
//           }
//           xhr.send(data);
//       }

   </script>
</head>
<body>
<center>
<form id="fLogin" action="" method="post" runat="server">
    <table id="title" border="0">
        <tr>
            <td>登录界面</td>
        </tr>
    </table>&nbsp;<table id="content" border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td style="height:50px;">
            </td>
            <td style="width:35%;">
            </td>
            <td style="width:173px">
            </td>
        </tr>
        <tr>
            <td class="descTd">用户名：</td>
            <td><input name="txtUName" type="text" id="txtUName" class="inputT" /></td>
            <td style="width: 173px" align="left" valign="middle"><div id="msgUname" class="<%=strNameErr%>">6-16个英文或数字</div></td>
        </tr>
        <tr>
            <td class="descTd">密&nbsp;&nbsp;&nbsp;码：</td>
            <td><input name="txtPwd" type="text" id="txtPwd" class="inputT" /></td>
            <td style="width: 173px" align="left"><div id="msgPwd" class="<%=strPwdErr%>">6-16个英文或数字</div></td>
        </tr>
<%--        <tr>
            <td class="descTd">验证码：</td>
            <td><input name="txtVCode" type="text" id="txtVCode" class="inputT" /></td>
            <td style="width: 173px" align="left"><img alt="VCode" id="vcode" onclick="ReVCode()" src="VCode.ashx" /></td>
        </tr>--%>
        <tr>
            <td >
                <input name="IsAlwayLogin" type="checkbox" value="1" />两天自动登录</td>
            <td>
                <input type="button"  id="btnL" value="登录" class="btnC" />
                <input type="button" id="btnC" value="取消" class="btnC" />
            </td>
            <td style="width: 173px"><a href="register.aspx">注  册</a></td>
        </tr>
        <tr>
            <td>
                </td>
            <td>
                </td>
            <td style="height:50px;"></td>
        </tr>
    </table>
    <input type="hidden" value="1" name="pback" />
</form>
</center>
</body>
</html>
